package nige.first;

import java.io.InputStream;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Display;
import javax.microedition.midlet.MIDlet;
import javax.microedition.rms.RecordStore;

import nige.portable.commun.MyCanvas;
import nige.portable.commun.Rendering;
import nige.portable.commun.Utils;

public class MyMidlet extends MIDlet {
	public void startApp() {
		Canvas canvas = new MyCanvas();
		canvas.setFullScreenMode(true);
		Display display = Display.getDisplay(this);
		display.setCurrent(canvas);
		Rendering rend = new Rendering(canvas);
		rend.start();
		
		try {
			InputStream is = getClass().getResourceAsStream("help.txt");
			StringBuffer sb = new StringBuffer();
			int chars;
			while ((chars = is.read()) != -1){
				sb.append((char) chars);
			}
			// System.out.println(sb);
		}
		catch(Exception e) {
			System.out.println(e);
		}
		try {
			RecordStore rs = RecordStore.openRecordStore("appliNicoGen",true);
			if(rs.getNumRecords()>0) {
				MyCanvas.AFF = new String(
						rs.getRecord(rs.getNextRecordID()-1));
				MyCanvas.AFF2 = ""+rs.getSizeAvailable();
			}
			/*
			String[] slist = RecordStore.listRecordStores();
			for(int i=0;i<slist.length;i++) {
				System.out.println(slist[i]);
			}
			rs.addRecord("test".getBytes(),0,4);
			System.out.println(rs.getNumRecords());
			System.out.println(rs.getNextRecordID());
			for(int i=0;i<rs.getNumRecords();i++) {
				System.out.println(rs.getRecord(i+1));
			}
			System.out.println(rs.getSizeAvailable());
			/*
			rs.addRecord(data, offset, numBytes);
			rs.getR
			*/
        }
		catch(Exception e) {
			System.out.println(e);
		}
	}
	// 67108722
	// 
	public void pauseApp() {
	}
	public void destroyApp(boolean unconditional) {
		try {
			RecordStore rs = RecordStore.openRecordStore("appliNicoGen",true);
			if(rs.getNumRecords()>0) {
				rs.deleteRecord(rs.getNextRecordID()-1);
			}
			String rec = Utils.getTime();
			rs.addRecord(rec.getBytes(),0, rec.length());
		}
		catch(Exception e) {
			System.out.println(e);
		}
	}
}
